#!/usr/bin/perl

require "./mailadmin-lib.pl";
&ReadParse();

my ($serv, %dft_uinfo, %msrvconf, @badmailfrom, $i, $tmp);

unless (&qmail_dir_check())
{
    &redirect('maildir_edit.cgi');
}

$serv = $in{'servradio'};
&get_dftuinfo(\%dft_uinfo);
&get_msrvconf(\%msrvconf);
@badmailfrom = &read_badmailfrom();

if ($serv eq 'mailserv')
{


    &header($text{'index_title'}, "", "mailserv_conf", 0, 1);
    print "<form action=save_mailserv.cgi name=mailform method=post>\n";

    print "<table class=mode1 border=0 width=100%>\n";

    printf "<tr><td width=15%>$text{'max_maillength'}</td><td><input name=databytes class=text size=8 value=\"%.2f\"><b> $text{'unit_MB'}</b></td></tr>\n", $msrvconf{'databytes'}/1048756;

    printf "<tr><td valign=top>$text{'badmailfrom'}</td>\n";
    print "<td width=10%><select name=unlist size=7 class=list4>\n";
    for ($i = 0; $i < @badmailfrom; $i++)
    {
	
	if ($badmailfrom[$i] =~ /^\@/)
	{
	    $tmp = $badmailfrom[$i];
	    $tmp =~ s/^@+//;
	    print "<option value=\"$badmailfrom[$i]\">[domains]  $tmp";
	}
	else
	{
	    print "<option value=\"$badmailfrom[$i]\">[address]  $badmailfrom[$i]";
	}
    }

    print "</select></td>\n";
    print "<td valign=top>\n";
    print "<table>\n";
    print "<tr><td>&nbsp;&nbsp;&nbsp;<input type=button class=button1 name=btn_aduname value=\"$text{'btn_adddomain'}\" onclick='fun_adddomain();'></td></tr>\n";
    print "<tr><td>&nbsp;&nbsp;&nbsp;<input type=button class=button1 name=btn_aduname value=\"$text{'btn_addaddr'}\" onclick='fun_addaddr();'></td></tr>\n";
    print "<tr><td>&nbsp;&nbsp;&nbsp;<input type=button class=button1 name=btn_rmuname value=\"$text{'btn_rmuname'}\" onclick='fun_rmuname();'></td></tr>\n";

    print "</table>\n";
    print "</td></tr>";


    print "<tr><td>&nbsp;</td><td>&nbsp;</td></tr>";
    print "<tr><td><b>$text{'dft_uinfo'}<b></td><td></td></tr>\n";
    print "<tr><td>&nbsp;&nbsp;&nbsp;$text{'dft_passwd'}</td><td><input name=passwd class=text value=\"$dft_uinfo{'upasswd'}\"></td></tr>\n";
    printf "<tr><td>&nbsp;&nbsp;&nbsp;$text{'dft_quota'}</td><td><input name=uquota class=text size=8 value=\"%.2f\"><b> $text{'unit_MB'}</b></td></tr>", $dft_uinfo{'uquota'}/1048756;
    print "</table>\n";

    print "<p><input type=submit name=btn_create class=button1 value=\"$text{'btn_save'}\" onclick=\'return mailsrv_save();\'>";

    $in{'unamecache'} = join(":", @badmailfrom);
    print "<input name=unamecache type=hidden value=\"$in{'unamecache'}\">";

    print "</form>\n";

} elsif ($serv eq 'smtpserv') {
    &header($text{'index_title'}, "", "index", 0, 1);
} elsif ($serv eq 'popserv') {
    &header($text{'index_title'}, "", "index", 0, 1);
} elsif ($serv eq 'webmailserv') {

    my ($port_used, $port, $i, $max_msgsize);

    &header($text{'index_title'}, "", "webmail_conf", 0, 1);

    $port = &get_wbmail_port();
    $max_msgsize = &get_wbmail_msgsize();

    print "<form action=save_webmail.cgi name=wbform method=post>\n";
    print "<table class=mode1 border=0 width=100%>\n";

    print "<tr><td width=15%>$text{'wb_port'}</td><td><input name=wb_port class=text size=10 value=\"$port\"></td></tr>";
    print "<tr><td>$text{'wb_maxmsg'}</td><td><select name=wb_maxmsg class=list1 size=1>\n";

    for ($i = 1; $i <= $config{'max_wbmail_msgsizeMB'} ; $i++)
    {
	printf "<option %s value=\"$i\">$i &nbsp;&nbsp; $text{'unit_MB'}", ($max_msgsize == $i) ? "selected":"";
    }

    print "</select>\n";
    print "<b></b></td></tr>";

    print "</table>\n";
    
    print "<p><input type=submit name=btn_create class=button1 value=\"$text{'btn_save'}\" onclick=\'return webmail_save();\'>";
    
    print "</form>\n";
}



print <<EOF;
<script>
function fun_adddomain()
{
    var tmp = prompt("$text{'msg_add_badmailfrom_domain'}","");
    var val, disp;

    val = ("@" + tmp);
    disp = ("[domains]  " + tmp);
    if (tmp)
    {
	
	i = document.mailform.unlist.options.length;
	document.mailform.unlist.options[i] = new Option(disp,val);
    }
    refresh_unamecache();
}

function fun_addaddr()
{
    var tmp = prompt("$text{'msg_add_badmailfrom_addr'}","");
    var val, disp;

    val = tmp;
    disp = ("[address]  " + tmp);
    if (tmp)
    {
	i = document.mailform.unlist.options.length;
	document.mailform.unlist.options[i] = new Option(disp,val);
    }
    refresh_unamecache();
}

function fun_aduname()
{
    var tmp = prompt("$text{'msg_add_badmailfrom'}","");
    if (tmp)
    {
	i = document.mailform.unlist.options.length;
	document.mailform.unlist.options[i] = new Option(tmp,tmp);
    }
    refresh_unamecache();
}

function fun_rmuname()
{
    var idx = document.mailform.unlist.selectedIndex;
    
    if ( idx >= 0)
    {
	document.mailform.unlist.options[idx] = null;
    }
    refresh_unamecache();
}

function refresh_unamecache()
{
    var str ="";
    for (i = 0; i < document.mailform.unlist.options.length; i++)
    {
	if (i < document.mailform.unlist.options.length - 1)
	    str += (document.mailform.unlist.options[i].value + ":");
	else
	    str += document.mailform.unlist.options[i].value;
    }
    document.mailform.unamecache.value = str;
}

function mailsrv_save()
{
    if (document.mailform.databytes.value == "")
    {
	alert(\"$text{'msg_databytes_empty'}\");
	return false;
    }

    if (document.mailform.passwd.value == "")
    {
	alert(\"$text{'msg_passwd_empty'}\");
	return false;
    }

    if (document.mailform.uquota.value == "")
    {
	alert(\"$text{'msg_uquota_empty'}\");
	return false;
    }

    document.mailform.action = "save_mailserv.cgi";
}


function webmail_save()
{
    if (document.wbform.wb_port.value == "")
    {
	alert(\"$text{'msg_wbmail_port_empty'}\");
	return false;
    }

    if (document.wbform.wb_maxmsg.value == "")
    {
	alert(\"$text{'msg_wbmail_maxmsg_empty'}\");
	return false;
    }

    document.wbform.action = "save_webmail.cgi";
}


</script>
EOF


&footer("", $text{"back_mailadmin"});
